home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / WANDR330.ZIP / SRC / SAVE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-01  |  4.9 KB  |  201 lines

  1. #include "wand_head.h"
  2. #include <errno.h>
  3. #include <curses.h>
  4.  
  5. extern char screen[NOOFROWS][ROWLEN+1];
  6. extern int saved_game;
  7. extern char screen_name[61];
  8. extern void crypt_file();
  9. extern int inform_me();
  10. extern WINDOW *messagewin;
  11.  
  12. struct saved_game {
  13.     short num;
  14.     long  score;
  15.     short bell;
  16.     short maxmoves;
  17.     short num_monsters;
  18. };
  19.  
  20. struct save_vars zz;
  21.  
  22. extern struct mon_rec start_of_list, *tail_of_list;
  23.  
  24. void save_game(num, score, bell, maxmoves)
  25. int  num, *bell, maxmoves;
  26. long *score;
  27. {
  28.     char fname[128], buf[70], *fp;
  29.     FILE *fo;
  30.     struct saved_game s;
  31.     extern char *getenv();
  32.     struct mon_rec *mp;
  33.  
  34.     if ((char *)NULL == (fp = getenv("SAVENAME"))) {
  35.     wclear(messagewin);
  36.     wmove(messagewin,0,0);
  37.     waddstr(messagewin,"Saving game filename? ");
  38.     wrefresh(messagewin);
  39.     wreadstring(messagewin,fname,20);
  40.     fp = fname;
  41.     }
  42.     wclear(messagewin);
  43.     wrefresh(messagewin);
  44.     if ((FILE *)NULL == (fo = fopen(fp, W_BIN))) {
  45.     perror(fp);
  46.     return;
  47.     }
  48.  
  49.     s.num = num;
  50.     s.score = *score;
  51.     s.bell = *bell;
  52.     s.maxmoves = maxmoves;
  53.     s.num_monsters = 0;
  54.  
  55.     mp = &start_of_list;        /* first entry is dummy    */
  56.     while (mp != tail_of_list) {
  57.     mp = mp->next;
  58.     s.num_monsters++;    /* count them monsters    */
  59.     }
  60.  
  61.     if ((1 != fwrite((char *)&s, sizeof(s), 1, fo)) ||
  62.     (1 != fwrite((char *)screen, sizeof(screen), 1, fo)) ||
  63.     (1 != fwrite((char *)&zz, sizeof(zz), 1, fo))) {
  64.     sprintf(buf,"Write error on '%s'\n", fname);
  65.     inform_me(buf,0);
  66.     fclose(fo);
  67.     return;
  68.     }
  69.  
  70.     mp = &start_of_list;
  71.     while (mp != tail_of_list) {
  72.     /* save them monsters    */
  73.     mp = mp->next;
  74.     if (1 != fwrite((char *)mp, sizeof(struct mon_rec), 1, fo)) {
  75.         sprintf(buf,"Write error on '%s'\n", fname);
  76.         inform_me(buf);
  77.         fclose(fo);
  78.         return;
  79.     }
  80.     }
  81.     fwrite(screen_name,sizeof(char),strlen(screen_name),fo);
  82.     fclose(fo);
  83. #ifndef NO_ENCRYPTION
  84.     crypt_file(fp);    /* encrpyt the saved game */
  85. #endif
  86. #ifndef FRIENDLY
  87.     clear();
  88.     CBON;
  89.     echo();
  90.     refresh();
  91.     endwin();
  92.     printf("Game saved.\n\nWANDERER Copyright (C) 1988 S. Shipway\n\n");
  93.     exit(0);
  94. #else
  95.    wmove(messagewin,0,0);
  96.    waddstr(messagewin,"Game saved.                   ");
  97.    wrefresh(messagewin);
  98.    return;
  99. #endif
  100. }
  101.  
  102. void restore_game(num, score, bell, maxmoves)
  103. int  *num, *bell, *maxmoves;
  104. long *score;
  105. {
  106.     FILE *fi;
  107.     struct saved_game s;
  108.     struct mon_rec *mp, *tmp, tmp_monst;
  109.     char fname[128], *fp;
  110.     extern char *getenv();
  111.  
  112.     if ((char *)NULL == (fp = getenv("SAVENAME"))) {
  113.     wclear(messagewin);
  114.     wmove(messagewin,0,0);
  115.     waddstr(messagewin,"Restoring game file name ? ");
  116.     wrefresh(messagewin);
  117.     echo(); CBOFF;
  118.     fp = fname;
  119.     gets(fp);
  120.     CBON; noecho();
  121.     }
  122.     wclear(messagewin);
  123.     wrefresh(messagewin);
  124. #ifndef NO_ENCRYPTION
  125.     crypt_file(fp);   /* decrypt it */
  126. #endif
  127.     if ((FILE *)NULL == (fi = fopen(fp, R_BIN))) {
  128.     endwin();
  129.     printf("Open error on '%s'\n", fp);
  130.     printf("Cannot restore game --- sorry.\n");
  131.     exit(1);
  132.     }
  133.     if ( (1 != fread((char *)&s, sizeof(s), 1, fi)) ||
  134.     (1 != fread((char *)screen, sizeof(screen), 1, fi)) ||
  135.     (1 != fread((char *)&zz, sizeof(zz), 1, fi)) ) {
  136.     endwin();
  137.     printf("Read error on '%s'n", fp);
  138.     printf("Cannot restore game --- sorry.\n");
  139.     fclose(fi);
  140.     exit(1);
  141.     }
  142.  
  143.     *num = s.num;
  144.     *score = s.score;
  145.     *bell = s.bell;
  146.     *maxmoves = s.maxmoves;
  147.  
  148.     /* free any monsters already on chain, to start clean */
  149.     mp = start_of_list.next;
  150.     while ((mp != NULL) && (mp != &start_of_list)) {
  151.     /* free them monsters    */
  152.     tmp = mp;
  153.     mp = mp->next;
  154.     free(tmp);
  155.     }
  156.  
  157.     /* re-initialize the monster list    */
  158.     /* start_of_list = {0,0,0,0,0,NULL,NULL}; */
  159.     start_of_list.x = 0;
  160.     start_of_list.y = 0;
  161.     start_of_list.mx = 0;
  162.     start_of_list.my = 0;
  163.     start_of_list.under = 0;
  164.     start_of_list.next = (struct mon_rec *)NULL;
  165.     start_of_list.prev = (struct mon_rec *)NULL;
  166.  
  167.     tail_of_list = &start_of_list;
  168.  
  169.     while (s.num_monsters--) {
  170.     /* use make_monster to allocate the monster structures    */
  171.     /* to get all the linking right without even trying    */
  172.     if ((struct mon_rec *)NULL == (mp = make_monster(0, 0))) {
  173.         endwin();
  174.         printf("Monster alloc error on '%s'n", fp);
  175.         printf("Try again - it might work.\nBut then,pigs might fly...\n");
  176.         fclose(fi);
  177.         exit(1);
  178.     }
  179.     if (1 != fread((char *)&tmp_monst, sizeof(struct mon_rec), 1, fi)) {
  180.         endwin();
  181.         printf("Monster read error on '%s'\n", fp);
  182.         printf("Cannot restore game --- sorry.\n");
  183.         fclose(fi);
  184.         exit(1);
  185.     }
  186.     /* copy info without trashing links    */
  187.     mp->x     = tmp_monst.x;
  188.     mp->y     = tmp_monst.y;
  189.     mp->mx    = tmp_monst.mx;
  190.     mp->my    = tmp_monst.my;
  191.     mp->under = tmp_monst.under;
  192.     }
  193.     if (fgets(screen_name,61,fi) == NULL)
  194.     *screen_name = '#';
  195.     fclose(fi);
  196. #ifndef FRIENDLY
  197.     unlink(fp);
  198. #endif
  199.     saved_game = 1;
  200. }
  201.